"""
Case Type   : 资源池化集群内核日志优化及数据校验
Case Name   : 踢出主机->failover过程中故障备二
Create At   : 2024/10/15
Owner       : cr13
Description :
    1.主机执行tpcc业务，备机执行读业务
    2.踢出主机，构建failover
    3.备一升主时故障备二
    4.查询备一日志，有关键字符
Expect      :
    1.成功
    2.成功
    3.成功
    4.有关键字符
History     :
"""
import os
import unittest
import time

from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.ComThread import ComThread

from yat.test import Node
from yat.test import macro


Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(3 != Primary_SH.get_node_num(), '非1+2环境不执行')
class OndemandHashmap(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.com = Common()
        self.constant = Constant()
        self.primary_sh = Primary_SH
        self.pri_com = Common('PrimaryDbUser')
        self.st1_sh = CommonSH('Standby1DbUser')
        self.st2_sh = CommonSH('Standby2DbUser')
        self.st1_com = Common('Standby1DbUser')
        self.st2_com = Common('Standby2DbUser')
        self.pri_root_node = Node(node="PrimaryRoot")
        self.pri_user_node = Node(node="PrimaryDbUser")
        self.st1_user_node = Node(node="Standby1DbUser")
        self.st2_user_node = Node(node="Standby2DbUser")
        self.st1_root_node = Node(node="Standby1Root")
        self.st2_root_node = Node(node="Standby2Root")

    def test_hashmap(self):
        step = '--step1:主机执行tpcc业务，备机执行读业务 expect:成功--'
        self.log.info(step)
        tpcc_thread = ComThread(
            self.com.start_tpcc, args=(self.pri_root_node, macro.TPCC_PATH, 'sh runBenchmark.sh pri_props.pg'))
        tpcc_thread.setDaemon(True)
        tpcc_thread.start()
        time.sleep(10)

        step = '--step2:故障主机，构建failover expect:成功--'
        self.log.info(step)
        mv_cmd = f"source {macro.DB_ENV_PATH};mv $GAUSSHOME/bin/gaussdb " \
                 f"$GAUSSHOME/bin/gaussdb_bak"
        self.log.info(mv_cmd)
        move_res = self.pri_user_node.sh(mv_cmd).result()
        self.log.info(move_res)
        self.assertIn('', move_res, "执行失败" + step)
        cmd = f" ps ux | grep -v grep | grep 'bin/gaussdb'"
        result = self.pri_com.get_sh_result(
            self.pri_user_node, cmd)
        self.log.info(result)
        pid1 = str(result.split()[1]).strip()
        self.log.info(pid1)
        cmd1 = f'kill -9 {pid1}'
        result = self.pri_com.get_sh_result(
            self.pri_user_node, cmd1)
        self.log.info(result)
        time.sleep(20)

        step = '--step3:备一升主时故障备二 expect:状态正常--'
        self.log.info(step)

        while True:
            self.pro_nod = self.primary_sh.get_reform_lock()
            if self.pro_nod == "6001":
                continue
            else:
                break

        if self.pro_nod == "6002":
            self.nodeuser1 = self.st2_user_node
            self.nodeuser = self.st1_user_node
            self.node = self.st1_root_node
            self.node1 = self.st2_root_node
            self.comsh = self.st1_sh
            self.comsh1 = self.st2_sh
            self.com = self.st1_com
            self.com1 = self.st2_com
        else:
            self.nodeuser = self.st2_user_node
            self.nodeuser1 = self.st1_user_node
            self.node = self.st2_root_node
            self.node1 = self.st1_root_node
            self.comsh = self.st2_sh
            self.comsh1 = self.st1_sh
            self.com = self.st2_com
            self.com1 = self.st1_com

        mv_cmd = f"source {macro.DB_ENV_PATH};mv $GAUSSHOME/bin/gaussdb " \
                 f"$GAUSSHOME/bin/gaussdb_bak"
        self.log.info(mv_cmd)
        move_res = self.nodeuser1.sh(mv_cmd).result()
        self.log.info(move_res)
        self.assertIn('', move_res, "执行失败" + step)

        cmd = f" ps ux | grep -v grep | grep 'bin/gaussdb'"
        result = self.com1.get_sh_result(
            self.nodeuser1, cmd)
        self.log.info(result)
        pid1 = str(result.split()[1]).strip()
        self.log.info(pid1)
        cmd1 = f'kill -9 {pid1}'
        result = self.com1.get_sh_result(
            self.nodeuser1, cmd1)
        self.log.info(result)
        time.sleep(20)

        step = '--step4:查询备一日志，有关键字符 expect:有关键字符--'
        cmd = f"source {macro.DB_ENV_PATH};ls -t $GAUSSLOG/pg_log/dn_{self.pro_nod} | head -1"
        result = self.com.get_sh_result(
            self.nodeuser, cmd)

        log_name = str(result).strip()
        self.log.info(log_name)
        cmd = f"source {macro.DB_ENV_PATH};grep '\[SS failover\]' $GAUSSLOG/pg_log/dn_{self.pro_nod}/{log_name}"
        result = self.com.get_sh_result(
            self.nodeuser, cmd)
        self.log.info(result)
        self.assertIn("[SS reform][SS failover] starts", result, "执行失败" + step)

    def tearDown(self):
        self.log.info('----恢复环境----')
        mvbk_cmd = f"source {macro.DB_ENV_PATH};mv $GAUSSHOME/bin/gaussdb_bak " \
                   f"$GAUSSHOME/bin/gaussdb"
        self.log.info(mvbk_cmd)
        move_res = self.nodeuser1.sh(mvbk_cmd).result()
        self.log.info(move_res)
        self.assertIn('', move_res, "执行失败")
        self.log.info(mvbk_cmd)
        move_res = self.pri_user_node.sh(mvbk_cmd).result()
        self.log.info(move_res)
        self.assertIn('', move_res, "执行失败")
        restart_res = self.primary_sh.restart_db_cluster()
        self.log.info(restart_res)
        switch_res = self.primary_sh.exec_cm_ctl('switchover', '-a')
        self.log.info(switch_res)
        self.log.info('-----停止tpcc业务-----')
        tpcc_pid = self.com.get_pid(self.pri_root_node,
                                    f'pri_props.pg')
        self.log.info(tpcc_pid)
        stop_tpcc = self.pri_root_node.sh(f'kill -9 {tpcc_pid}').result()
        self.log.info(stop_tpcc)
        self.log.info(f'----{os.path.basename(__file__)} end')
